package com.fbty.mt.action.admin;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import com.fbty.base.action.BaseAction;
import com.fbty.mt.domain.CheckInfo;
import com.fbty.mt.domain.ErrorIpInfo;
import com.fbty.mt.domain.IpInfo;
import com.fbty.mt.domain.OnlyIpInfo;
import com.fbty.mt.service.IpService;
import com.fbty.mt.service.ProxyService;
import com.fbty.mt.utils.IPInfoHelper;
import com.fbty.mt.utils.IpTxt;
import com.fbty.mt.utils.OnlyIPTxt;
import com.fbty.mt.utils.SingletonThreadPool;
import com.fbty.mt.utils.StaticValue;

public class FileUploadAction extends BaseAction {
	private static final long serialVersionUID = 9123621222778580392L;
	// 上传文件
	private File file;
	// 上传文件名
	private String fileName;
	//private String savePath; 
	// 处理后显示的路径
	private String savePath;
	private IpService ipInfoService;
	private Integer id;
	private ProxyService proxyService;
	private String type; //上传类型 0 代表 txt 1代表excel
	/**
	 * 共享类ip
	 * @return
	 * @throws Exception
	 */
	public String fileupload(){
		if(file == null) 
		return "nofile";
		StaticValue.uploadTxt.clear();
		StaticValue.errrorIp.clear();
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String str="";
			int i=0;
			while ((str=br.readLine()) != null) {
				i++;
				String[] args = str.split("\\|");
				if(args.length==6){
					StaticValue.uploadTxt.add(new IpTxt(args[0].trim(),Integer.parseInt(args[1].trim()),args[2].trim().toLowerCase(),
							args[3].trim(),args[4].trim(),args[5].trim()));
				}else{
					StaticValue.errrorIp.add(str);
				}
			}
			br.close();
			//sessionMap.put("totalLine", StaticValue.uploadTxt.size());
			sessionMap.put("totalLine", i);
			//logger.debug("上传文件IP总条数："+StaticValue.uploadTxt.size());
			new Thread(){
				public void run() {
					while(StaticValue.uploadTxt.size()>0){
						synchronized (StaticValue.obj) {
							IpTxt line = StaticValue.uploadTxt.pop();
							if(line!=null){
								saveIp(line);
							}
						}
					}
				}
			}.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
		
	}
		/**
		 * 
		 * Description: 获取延迟<br>
		 * @param line
		 */
		private  void  saveIp(IpTxt line){
			String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."                  
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."                  
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."                  
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; 
			if(line.getIp().matches(regex)){
				IpInfo info = new IpInfo();
				info.setIp(line.getIp());
				info.setPort(line.getPort()+"");
				info.setState(1);
				info.setType(line.getType().toUpperCase());
				info.setCity(line.getCity());
				info.setCountry(line.getCountry());
				info.setRegion(line.getRegion());
				info.setSpeed("0.21");
				info.setUpdateTime(new Date());
				StaticValue.ipInfoes.add(info);
			}else{
				/*ErrorIpInfo errorIpInfo = new ErrorIpInfo();
				errorIpInfo.setIp(line.getIp());
				errorIpInfo.setPort(line.getPort()+"");
				errorIpInfo.setType(line.getType());
				errorIpInfo.setUploadTime(new Date());
				errorIpInfo.setState(1);
				errorIpInfo.setCity(line.getCity());
				errorIpInfo.setCountry(line.getCountry());
				errorIpInfo.setRegion(line.getRegion());*/
				StaticValue.errrorIp.add(line.toString());
			}
		}
	/**
	 * 独享类ip 文件上传
	 * @return
	 * @throws Exception
	 */
	public String fileOnlyUpload(){
		if(file == null) 
			return "nofile";
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));
			String str="";
			while ((str=br.readLine()) != null) {
				String[] args = str.split("\\|");
				if(args.length==6){
					OnlyIPTxt only= new OnlyIPTxt();
					String[] ips = args[0].split(":");
					only.setExportIp(ips[0]);
					only.setExportPort(Integer.parseInt(ips[1]));
					only.setCountry(args[1]);
					only.setRegion(args[2]);
					only.setCity(args[3]);
					only.setType(args[4]);
					//only.setProxyType(args[5]);
					only.setPrice(Double.parseDouble(args[5]));
					StaticValue.uploadOnlyTxt.add(only);
				}
			}
			br.close();
			//存放本次上传 的总条数
			sessionMap.put("totalOnlyLine", StaticValue.uploadOnlyTxt.size());
			logger.debug("上传文件IP总条数："+StaticValue.uploadOnlyTxt.size());
			//ExecutorService pool = SingletonThreadPool.getInstance().getPool();
			for(int i=0;i<5;i++){
				new Thread(){
					public void run() {
						while(StaticValue.uploadOnlyTxt.size()>0){
							OnlyIPTxt line = StaticValue.uploadOnlyTxt.pop();
							if(line!=null){
								saveOnlyIp(line);
							}
						}
					}
				}.start();
			/*	pool.execute(new Runnable() {
					@Override
					
						Thread.yield();
						if(StaticValue.uploadIpInfoes.size()>0){
							ipInfoService.saveIpInfos(StaticValue.uploadIpInfoes);
							StaticValue.uploadIpInfoes.clear();
						}
					}
				});*/
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	/**
	 * 
	 * Description: 获取延迟<br>
	 * @param line
	 */
	private  void  saveOnlyIp(OnlyIPTxt line){
		String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."                  
			+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."                  
			+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."                  
			+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"; 
		if(line.getExportIp().matches(regex)){
			OnlyIpInfo info = new OnlyIpInfo();
			info.setType(line.getType());
			info.setCity(line.getCity());
			info.setCountry(line.getCountry());
			info.setRegion(line.getRegion());
			info.setExportIp(line.getExportIp());
			info.setExportPort(line.getExportPort());
			//info.setProxyType(line.getProxyType());
			info.setPrice(line.getPrice());
			/*Integer  type = 0;
			if("socks5".equalsIgnoreCase(info.getType())) {
				type = CheckInfo.SOCKS5_PROXY;
			} else if("socks4".equalsIgnoreCase(info.getType())) {
				type = CheckInfo.SOCKS4_PROXY;
			} else if("http".equalsIgnoreCase(info.getType())) {
				type = CheckInfo.HTTP_PROXY;
			} else if("https".equalsIgnoreCase(info.getType())) {
				type = CheckInfo.HTTPS_PROXY;
			}*/
			/*CheckInfo checkInfo = proxyService.checkConnection(info.getEntranceIp(), info.getEntrancePort(), type);
			if(checkInfo.getReslut() == 0) {
				info.setSpeed((double)checkInfo.getDelay()/1000+"");
				info.setUploadTime(new Date());
				StaticValue.uploadOnlyIps.add(info);
			}else{
				StaticValue.errorOnlyTxt.add(line);
			}*/
			info.setSpeed("0.11");
			info.setUploadTime(new Date());
			StaticValue.uploadOnlyIps.add(info);
		}else{
			StaticValue.errorOnlyTxt.add(line);
		}
	}
		
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}


	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}



	public String getFileName() {
		return fileName;
	}



	public void setFileName(String fileName) {
		this.fileName = fileName;
	}


	public IpService getIpInfoService() {
		return ipInfoService;
	}

	public void setIpInfoService(IpService ipInfoService) {
		this.ipInfoService = ipInfoService;
	}
	public ProxyService getProxyService() {
		return proxyService;
	}
	public void setProxyService(ProxyService proxyService) {
		this.proxyService = proxyService;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

}
